package com.RabbitMQ.routing.Consumer;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by thinkjoy on 2017/6/29.
 */
public class ReceiveLogsDirectOne {
    private static final String EXCHANGE_NAME = "direct_logs";
    // 路由关键字
    private static final String[] routingKeys = new String[]{"info" ,"warning"};

    public static void main(String[] args) throws IOException,TimeoutException{
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection=factory.newConnection();
        Channel channel=connection.createChannel();
        //声明交换器
        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
//        获取匿名队列名称
        String queuename=channel.queueDeclare().getQueue();
//        根据路由关键字进行绑定
        for (String rountingkey:routingKeys) {
            channel.queueBind(queuename,EXCHANGE_NAME,rountingkey);
            System.out.println("ReceiveLogsDirectOne exchange:"+EXCHANGE_NAME+"," +
                    " queue:"+queuename+", BindRoutingKey:" + rountingkey);
        }
        System.out.println("ReceiveLogsDirectOne  Waiting for messages");

        Consumer consumer=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message=new String(body,"UTF-8");
                System.out.println("ReceiveLogsDirect1 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
            }
        };
        channel.basicConsume(queuename,true,consumer);
    }
}
